window.onload = function () {

}

var layer, table;
layui.use(['layer', 'laydate', 'table'], function () {
    $ = layui.jquery;
    var layer = layui.layer,
        laydate = layui.laydate,
        table = layui.table;

    laydate.render({
        elem: '#dateRange'
        , range: true
    });

    // 初始化表格
    table.render({
        //设置ID
        id: 'bulletinTable',
        // 指定原始 table 容器
        elem: '#bulletinTableId',
        // 异步数据接口
        url: '/bulletin/getBulletins',
        where: {bulletinTitle: '', publisherName: '', dateRange: ''},
        // 设置导出按钮
        toolbar: '#toolBar',
        //设置表头。值是一个二维数组
        cols: [
            [{
                type: 'numbers',
                width: 50,
                title: '序号',
                fixed: "left"
            }, {
                field: 'bulletinTitle',
                minWidth: 250,
                title: '公告标题',
                align: "center",
                event: 'viewBulletin',
            }, {
                field: 'bulletinContent',
                width: 300,
                title: '公告内容',
                align: "center"
            }, {
                field: 'realName',
                width: 100,
                title: '发布者',
                align: "center"
            }, {
                field: 'readCount',
                width: 80,
                title: '阅读数',
                align: "center"
            }, {
                field: 'gmtCreateStr',
                width: 180,
                title: '发布时间',
                align: "center"
            }, {
                field: 'gmtModifyStr',
                width: 180,
                title: '修改时间',
                align: "center"
            }, {
                //     field: 'grade',
                //     width: 100,
                //     title: '年级',
                //     align: "center"
                // }, {
                title: '操作',
                minWidth: 150,
                templet: '#bulletinTableBar',
                fixed: "right",
                align: "center"
            }]
        ],
        // 每页条数的选择项
        limits: [5, 10, 15, 20, 25, 50, 100],
        // 每页显示的条数
        limit: 15,
        // 是否开启分页
        page: true
    });

    table.on('toolbar(bulletinTableFilter)', function (obj) {
        var param = obj.data;
        if (obj.event === 'add') {
            var url = '/pushBulletin';
            window.location.href = url;
        }
    });

    table.on('rowDouble(bulletinTableFilter)', function (obj) {
        var data = obj.data;//获取点击行数据
        var url = '/viewBulletin?bulletinId=' + data.bulletinId;
        window.location.href = url;
    });

    // 监听表格编辑、删除按钮
    table.on('tool(bulletinTableFilter)', function (obj) {
        var param = obj.data;
        console.log(param);
        if (obj.event === 'modify') {
            var url = '/modifyBulletinInfo?bulletinId=' + param.bulletinId;
            window.location.href = url;
        } else if (obj.event === 'delete') {
            layer.confirm('真的删除该公告吗?', function (index) {
                removeBulletin(param);
                layer.close(index);
            });
        }
    });

    $('#searchBtn').on('click', function () {
        //执行搜索重载
        table.reload('bulletinTable', {
            page: {
                curr: 1
            },
            where: {
                bulletinTitle: $("#bulletinTitle").val(),
                publisherName: $("#publisherName").val(),
                dateRange: $("#dateRange").val()
            }
        }, 'data');
    });

    function searchBulletinInfo() {
        //执行搜索重载
        table.reload('bulletinTable', {
            page: {
                curr: 1
            },
            where: {
                bulletinTitle: $("#bulletinTitle").val(),
                publisherName: $("#publisherName").val(),
                dateRange: $("#dateRange").val()
            }
        }, 'data');
    }

    function removeBulletin(data) {
        var param = {
            bulletinId: data.bulletinId
        }
        $.ajax({
            type: "POST",
            url: "/bulletin/removeBulletin",
            //contentType: "application/json",
            sync: true,
            data: param,
            dataType: "json",
            success: function (result) {
                if (result.code == 200) {
                    searchBulletinInfo();
                    layer.msg('删除成功！', {icon: 1, offset: '100px', time: 2000});
                } else {
                    layer.msg(result.msg, {icon: 2, offset: '100px', time: 2000});
                }
            }
        });
    }
});

